<?php
/**
 * 销售人员相关操作模型
 * SoleModel.class.php
 *
 * @author 	王中艺
 * @date 	2017-02-2-04
 */

namespace Wechat\Model;

class SoleModel extends BaseModel {

	//重定义数据表明
	protected   $trueTableName = 't_user_sole_info';
	
	/**
	 * 更新用户信息
	 * @param   $userId 用户ID
	 * @param   $truename 真实姓名
	 * @param   $mobile 手机号码
	 * @param   $qq QQ号
	 * @param   $region 区域
	 * @param   $schoolName 学校名称
	 * @param   $schoolStudents 学生数量
	 * @param   $schoolTeachers 教师数量
	 * @param 	$schStuFee 	学生学费
	 * @return  boolean
	 */
	function updateSoleInfo($userId, $truename, $mobile, $qq, $region, $schoolName, $schoolStudents, $schoolTeachers, $schStuFee){
		$baseInfo 	= array(
			'truename'	=> $truename,
			'mobile'	=> $mobile
		);
		D('User')->updateUserInfo($userId, $baseInfo);

		$soleInfo 	= array(
			'user_id'	=> $userId,
			'qq'		=> $qq,
			'region'	=> $region,
			'school_name'	=> $schoolName,
			'school_students'	=> $schoolStudents,
			'school_teachers'	=> $schoolTeachers,
			'school_stu_fee'	=> $schStuFee
		);
		$mSole 	= M('t_user_sole_info', null);
		$isExt 	= $mSole->where('user_id = %d', $userId)->find();
		if($isExt){
			$ret 	= $mSole->save($soleInfo);
		}else{
			$ret 	= $mSole->add($soleInfo);
		}
		if($ret === false)
			$this->errMsg 	= '数据更新失败!(server)';

		//清除缓存
		S('sole_info_'.$userId, null);

		return $ret;
	}

	/**
	 * 获取销售人员信息（部分）
	 * @param   $userId 用户ID
	 * @return  array(info)
	 */
	function getSoleInfo($userId){
		$cacheKey 	= 'sole_info_'.$userId;
		$soleInfo 	= S($cacheKey);
		if(!$soleInfo){
			$soleInfo 	= $this->where('user_id = %d', $userId)->find();
			if($soleInfo) S($cacheKey, $soleInfo);
		}

		return $soleInfo;
	}
}